package com.xqboss.apps.controller.user;

import com.xqboss.apps.service.user.UserIdentityRankService;
import com.xqboss.apps.vo.user.IdentityRankUpgradeVo;
import com.xqboss.apps.vo.user.PromotionSuccessVo;
import com.xqboss.apps.vo.user.PromotionVo;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.constant.HttpStatus;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import com.xqboss.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 用户身份-段位(UserIdentityRank)表控制层
 *
 * @author lujiahong
 * @since 2023-12-05 12:04:08
 */
@Slf4j
@Api(tags = "用户身份-段位管理")
@RestController
@RequestMapping("/user/identityRank")
public class UserIdentityRankController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private UserIdentityRankService userIdentityRankService;

    @ApiOperation("身份晋升详情")
    @GetMapping("identityPromotionDetails")
    public R<Map<String, PromotionVo>> identityPromotionDetails(){
        try {
            Map<String, PromotionVo> promotionVoMap = this.userIdentityRankService.identityPromotionDetails(getUserId());
            return R.ok(promotionVoMap);
        }catch (Exception e){
            log.error("[identityPromotionDetails exception]",e);
            return R.fail(HttpStatus.ERROR,e.getMessage());
        }
    }

    @ApiOperation("身份晋升确认")
    @Log(title = "身份晋升确认", businessType = BusinessType.INSERT)
    @RepeatSubmit(interval = 1000, justUrl = true)
    @PostMapping("identityPromotionConfirm")
    public R<PromotionSuccessVo> identityPromotionConfirm(@Validated @RequestBody IdentityRankUpgradeVo vo){
        try {
            return this.userIdentityRankService.identityPromotionConfirm(vo.getSpendNftType(),getUserId());
        }catch (ServiceException e){
            log.error("[identityPromotionConfirm exception]",e);
            return R.fail(HttpStatus.ERROR,e.getMessage());
        }catch (Exception e){
            //清除redis缓存的晋升信息
            this.userIdentityRankService.clearRedisCacheIdentityRankInfo();
            log.error("[identityPromotionConfirm exception]",e);
            return R.fail(HttpStatus.ERROR,e.getMessage());
        }
    }




}

